const app = getApp()
const util = require("../../utils/utils.js")

// pages/forum-home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    viewType: 0,
    posts: [],
    headimgs: [],
    nicknames: [],
    likes: [],
    myComments: [],
    myInfo: null,
    isCommentPopupShow: false,
    commentTextareaValue: "",
    targetIndex: "",
    loginUserInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      viewType: app.globalData.viewPostType
    })
    if (this.data.viewType == 1) {
      wx.setNavigationBarTitle({
        title: "我的贴子"
      });
      this.initData(1)
    }
    if (this.data.viewType == 2) {
      wx.setNavigationBarTitle({
        title: "我点赞的"
      });
      this.initData(2)
    }
    if (this.data.viewType == 3) {
      wx.setNavigationBarTitle({
        title: "我评论的"
      });
      this.initData(3)
    }
    this.setData({
      loginUserInfo: app.globalData.userInfo
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onPostTapped: function (event) {
    app.globalData.postingId = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../forum-detail/detail'
    });
  },
  preview(event) {
    wx.previewImage({
      current: this.data.posts[event.currentTarget.dataset.iindex].photo[event.currentTarget.dataset.index],
      urls: this.data.posts[event.currentTarget.dataset.iindex].photo,
    })
  },
  doILikeIt: function (type) {
    var loginAccount = app.globalData.loginAccount
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/doILikeThem',
      method: 'GET',
      data: {
        type: type,
        username: loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          likes: res.data.data
        })
      }
    })
  },
  doLike: function (event) {
    //console.log("do like");
    var loginAccount = app.globalData.loginAccount
    var iindex = event.currentTarget.dataset.iindex
    var postingId = this.data.posts[iindex].id
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/doLike',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        var iLike = "likes[" + iindex + "]"
        var iPost = "posts[" + iindex + "].likes"
        var likes = that.data.posts[iindex].likes + 1
        that.setData({
          [iLike]: 1,
          [iPost]: likes
        })
      }
    })
  },
  dontLike: function (event) {
    //console.log("dont like");
    var loginAccount = app.globalData.loginAccount
    var iindex = event.currentTarget.dataset.iindex
    var postingId = this.data.posts[iindex].id
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/dontLike',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        var iLike = "likes[" + iindex + "]"
        var iPost = "posts[" + iindex + "].likes"
        var likes = that.data.posts[iindex].likes - 1
        that.setData({
          [iLike]: 0,
          [iPost]: likes
        })
      }
    })
  },
  showCommentPopup: function (event) {
    let index = event.currentTarget.dataset.iindex
    console.log(index);
    this.setData({
      isCommentPopupShow: true,
      targetIndex: index
    })
    // console.log(app.globalData.loginAccount);
  },

  hideCommentPopup: function () {
    this.setData({
      isCommentPopupShow: false
    })
  },
  updateCommentInputData: function (event) {
    this.setData({
      commentTextareaValue: event.detail.value
    });
  },
  sendComment: function (event) {
    //console.log("do like");
    let loginAccount = app.globalData.loginAccount
    let index = this.data.targetIndex
    let postingId = this.data.posts[index].id
    let content = this.data.commentTextareaValue
    var that = this
    if (content.match(/^[ ]*$/)) {
      wx.showToast({
        title: '评论不能为空',
        icon: 'error',
        duration: 1500
      });
      return;
    }
    wx.request({
      url: 'http://localhost:8888/comments/insertComments',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId,
        content: content
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        var commentVar = "posts[" + index + "].comments";
        var commentNum = that.data.posts[index].comments + 1;
        that.setData({
          [commentVar]: commentNum
        });
        that.hideCommentPopup();
        that.setData({
          commentTextareaValue: ""
        });
        wx.showToast({
          title: '评论发送成功!',
          icon: 'success',
          duration: 2000
        });
      }
    })
  },
  showLoginComment() {
    wx.showToast({
      title: '登录后方可评论',
      icon: "error"
    })
  },
  showLoginLike() {
    wx.showToast({
      title: '登录后方可点赞',
      icon: "error"
    })
  },
  onDelete(e) {
    //console.log("delete");
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var posts = this.data.posts
    var headimgs = this.data.headimgs
    var nicknames = this.data.nicknames
    var likes = this.data.likes
    var that =this
    wx.request({
      url: 'http://localhost:8888/posting/deletePosting',
      method: 'POST',
      data: {
        id: id
      },
      header: {
          'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        posts.splice(index, 1)
        headimgs.splice(index, 1)
        nicknames.splice(index, 1)
        likes.splice(index, 1)
        that.setData({
          posts: posts,
          headimgs: headimgs,
          nicknames: nicknames,
          likes: likes
        })
        wx.showToast({
          title: '删除成功',
          icon: 'success'
        })
      }
  })
  },
  onEdit(e) {
    //console.log("edit");
    app.globalData.postIsEdit = true
    app.globalData.editPostingId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../post-new/edit',
    })
  },
  initData(type) {
    var that = this
    if (type == 1) {
      wx.request({
        url: 'http://localhost:8888/posting/getMyPosting',
        method: 'GET',
        data: {
          user: app.globalData.loginAccount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            posts: res.data.data
          })
          for (var i = 0; i < that.data.posts.length; i++) {
            var iPhoto = "posts[" + i + "].photo"
            var iTag = "posts[" + i + "].tag"
            that.setData({
              [iPhoto]: util.strToArr(that.data.posts[i].photo),
              [iTag]: util.strToArr(that.data.posts[i].tag)
            })
          }
          that.doILikeIt(1)
        }
      })
      wx.request({
        url: 'http://localhost:8888/posting/getPostingUserHeadImg',
        method: 'GET',
        data: {
          type: 1,
          user: app.globalData.loginAccount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            headimgs: res.data.data
          })
        }
      })
      wx.request({
        url: 'http://localhost:8888/posting/getPostingUserName',
        method: 'GET',
        data: {
          type: 1,
          user: app.globalData.loginAccount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            nicknames: res.data.data
          })
        }
      });
    }
    if (type == 2) {
      wx.request({
        url: 'http://localhost:8888/posting/getThoseILike',
        method: 'GET',
        data: {
          user: app.globalData.loginAccount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            posts: res.data.data
          })
          for (var i = 0; i < that.data.posts.length; i++) {
            var iPhoto = "posts[" + i + "].photo"
            var iTag = "posts[" + i + "].tag"
            that.setData({
              [iPhoto]: util.strToArr(that.data.posts[i].photo),
              [iTag]: util.strToArr(that.data.posts[i].tag)
            })
          }
          that.doILikeIt(2)
        }
      })
      wx.request({
        url: 'http://localhost:8888/posting/getPostingUserHeadImg',
        method: 'GET',
        data: {
          type: 2,
          user: app.globalData.loginAccount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            headimgs: res.data.data
          })
        }
      })
      wx.request({
        url: 'http://localhost:8888/posting/getPostingUserName',
        method: 'GET',
        data: {
          type: 2,
          user: app.globalData.loginAccount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            nicknames: res.data.data
          })
        }
      });
    }
    if (type == 3) {
      wx.request({
        url: 'http://localhost:8888/comments/getCommentsByUser',
        method: 'GET',
        data: {
          user: app.globalData.loginAccount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            myComments: res.data.data
          })
        }
      })
      wx.request({
        url: 'http://localhost:8888/account/getAccount',
        method: 'GET',
        data: {
          id: app.globalData.loginAccount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            myInfo: res.data.data
          })
        }
      })
    }
  },
  viewPostDetail(e) {
    app.globalData.postingId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../forum-detail/detail'
    });
  },
  deleteComment(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    wx.request({
      url: 'http://localhost:8888/comments/deleteComments',
      method: 'POST',
      data: {
        id: id
      },
      header: {
          'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var myComments = that.data.myComments
        myComments.splice(index, 1)
        that.setData({
          myComments: myComments
        })
        wx.showToast({
          title: '删除成功',
          icon: 'success'
        })
      }
  })
  }
})